package com.jeehentor.system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeehentor.common.api.vo.LoginUser;
import com.jeehentor.common.vo.Result;
import com.jeehentor.dto.IdsRequestDTO;
import com.jeehentor.dto.QuerySysUserDTO;
import com.jeehentor.system.service.ISysUserService;
import com.jeehentor.utils.SecurityUtil;
import com.jeehentor.vo.UserVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author wzd
 * @since 2024-04-08
 */
@RestController
@RequestMapping("/user")
@Tag(name = "用户管理")
@RequiredArgsConstructor
public class SysUserController {

    private final ISysUserService sysUserService;
    private final ISysUserService iSysUserService;


    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginUser user, HttpServletRequest request){
        return sysUserService.login(user,request);
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Result<String> logout(HttpServletRequest request){
        sysUserService.logout(request);
        return Result.ok("退出登录成功");
    }

    @Operation(summary = "注册")
    @PostMapping("/register")
    public Result<String> register(@RequestBody LoginUser user){
        sysUserService.register(user);
        return Result.ok("注册成功");
    }
    @Operation(summary = "修改密码")
    @PostMapping("/updatePwd")
    public Result<String> updatePwd(@RequestBody LoginUser user){
        sysUserService.updatePwd(user);
        return Result.ok("修改成功");
    }
    @Operation(summary = "修改基本信息")
    @PostMapping("/updateUserInfo")
    public Result<String> updateUserInfo(@RequestBody LoginUser user){
        sysUserService.updateUserInfo(user);
        return Result.ok("修改成功");
    }

    @Operation(summary = "修改用户")
    @PostMapping("/updateUser")
    public Result<String> updateUser(@RequestBody LoginUser user){
        sysUserService.updateUser(user);
        return Result.ok("修改成功");
    }

    @Operation(summary = "删除用户")
    @PostMapping("/delUser")
    public Result<String> delUser(@RequestBody IdsRequestDTO ids){
        sysUserService.delUser(ids);
        return Result.ok("删除成功");
    }

    @Operation(summary = "重置密码")
    @PostMapping("/restPwd")
    public Result<String> restPwd(@RequestBody LoginUser user){
        sysUserService.restPwd(user);
        return Result.ok("重置密码成功");
    }

    @Operation(summary = "用户信息")
    @PostMapping("/userInfo")
    public Result<LoginUser> register(){
        LoginUser currentUser = SecurityUtil.getCurrentUser();
        if(CollectionUtils.isNotEmpty(currentUser.getRoles())){
            ArrayList<String> roleLabel = new ArrayList<>();
            currentUser.getRoles().forEach(x->{
                roleLabel.add(iSysUserService.getRoleByCode(x));
            });
            currentUser.setRolesLabel(roleLabel);

        }

        return Result.ok(currentUser);
    }

    @Operation(summary = "用户列表（不包含组织子级）")
    @PostMapping("/pageList")
    public Result<Page<UserVO>> pageList(@RequestBody QuerySysUserDTO user){
        Page<UserVO> page = sysUserService.pageList(user);
        return Result.ok(page);
    }

    @Operation(summary = "用户列表（包含组织子级）")
    @PostMapping("/pageListByOrg")
    public Result<Page<UserVO>> pageListByOrg(@RequestBody QuerySysUserDTO user){
        Page<UserVO> page = sysUserService.pageListByOrg(user);
        return Result.ok(page);
    }

}
